﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T07
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("两数相加：{0}",Mymath.Sum(1,2));
            Console.WriteLine("多数数相加：{0}", Mymath.Sum(1, 2,3,4));
            Console.WriteLine("多数数相加：{0}", Mymath.Sum(1.0,2.0));
            int[] num = { 1, 2, 3, 4, 9, 8 };
            int? maxindex = null; //out和ref 的不同在于，ref在引用前必须赋值，而out可以直接引用
            int? max = Mymath.Max(num,ref maxindex);
            Console.WriteLine("最大数为{0},索引为{1}",max,maxindex);
            int? max1 = Mymath.Max(new int[0],ref maxindex);
            Console.WriteLine("最大数为{0},索引为{1}", max1, maxindex);
            int? minindex; //out和ref 的不同在于，ref在引用前必须赋值，而out可以直接引用
            int? min = Mymath.Min(num, out minindex);
            Console.WriteLine("最小数为{0},索引为{1}", max, maxindex);
            int? min1 = Mymath.Min(new int[0], out minindex);
            Console.WriteLine("最小数为{0},索引为{1}", max1, maxindex);
            Console.ReadKey();
           
        }
    }
}
class Mymath 
{/// <summary>
/// 求两个操作数的和    
/// </summary>
/// <param name="a">操作数一</param>
/// <param name="b">操作数二</param>
/// <returns>返回和</returns>
    public static int Sum(int a, int b)
    {
        int result = 0;
        result = a + b;
        return result;
    }
    public static int? Sum(params int[] values)//params  可延伸参数列表
    {
        int? result=null;//？表示可空，用于防止Sum();返回0：
        if (values.Length > 0)
        {
            result = 0;
            foreach (var value in values)
                result += value;
        }
        return result;
    }
    public static double? Sum(params double[] values)
    {
        double? result = null;
        if (values.Length > 0)
        {
            result = 0;
            foreach (var value in values)
                result += value;
        }
        return result;
    }
    public static int? Max( int[] values, ref int? maxindex)//ref的使用，引用参数
    { int? max;
        if (values.Length <= 0)
        {

            maxindex = null;
            return null;
        }
        else
        {
            max = 0;
            maxindex = 0;
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] > max)
                {
                    max = values[i];
                    maxindex = i;
                }

            }
        }
        return max;
    }
    public static int? Min(int[] values, out int? minindex)//out的使用,输出参数
    {
        int? min;
        if (values.Length <= 0)
        {

            minindex = null;
            return null;
        }
        else
        {
            min = 0;
            minindex = 0;
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] < min)
                {
                    min = values[i];
                    minindex = i;
                }

            }
        }
        return min;
    }
}